package org.jeecgframework.web.controller;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mybatis.ext.WiseCoreService;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.ComboTree;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.model.json.Highchart;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.*;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.WxCommonService;
import org.jeecgframework.web.service.ZyCommonService;
import org.jeecgframework.web.service.ZyContactsServiceI;
import org.jeecgframework.web.service.impl.ZyContactsServiceImpl;
import org.jeecgframework.web.system.pojo.base.*;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.StaticValue;
import org.jeecgframework.web.util.sendWxMessage;
import org.jeecgframework.workflow.pojo.base.TSBaseBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fr.third.org.apache.poi.hssf.record.formula.functions.IsNa;



/**
 * @Title: Controller
 * @Description: 故障率报表
 * @author mhg
 * @date 2018-6-25
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/zyReportController")
public class ZyReportController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ZyContactsController.class);

	@Autowired
	private SystemService systemService;
	@Autowired
	private WiseCoreService wiseCoreService;
	@Autowired
	private ZyCommonService zyCommonService;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}




	/**
	 * @author mhg
	 * 2018-6-25
	 * Description 返回备件类型 tree
	 */
	@ResponseBody
	@RequestMapping(params="getEsTypesTreeForIrReport")
	public List<ComboTree> getEsTypesTreeForIrReport(){
		return zyCommonService.getEsTypesTreeForIrReport();
	}

	/**
	 * 故障率页面
	 *
	 * @return
	 */
	@RequestMapping(params = "faultRateReport")
	public ModelAndView zyInspectionRecord(HttpServletRequest request) throws Exception{
		Date nowDate=new Date();
		//获取当前年份
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy");
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM");
		String yearStr=sdf.format(nowDate);
		String year=yearStr.substring(2,4);
		request.setAttribute("year",year);

		//当前年份一月份
		String oneMonthStr=yearStr+"-01";
		Date beginDate=sdf2.parse(oneMonthStr);
		request.setAttribute("beginDate",beginDate);

		//获取当前日期
		Date endDate=new Date();
		request.setAttribute("endDate",endDate);



		return new ModelAndView("com/zybk/zy_report/faultRateReport");
	}


	@RequestMapping(params="queryFaultRateReportData")
	@ResponseBody
	public AjaxJson queryFaultRateReportData(HttpServletRequest req) throws Exception{
		AjaxJson json = new AjaxJson();
		String cinemaLine=req.getParameter("cinemaLine");
		String esTypeIds=req.getParameter("esTypeIds");
		String bjTypeIds=req.getParameter("bjTypeIds");
		String tb=req.getParameter("tb");
		String bps=req.getParameter("bp");
		String countDate_begin=req.getParameter("countDate_begin");
		String countDate_end=req.getParameter("countDate_end");

		List<Highchart> highCharts = zyCommonService.queryFaultRateReportData(cinemaLine, esTypeIds,bjTypeIds,tb,bps,countDate_begin,countDate_end);
		json.setObj(highCharts);//table + 折线图
		return json;
	}


	/**
	 * @author mhg
	 * 2018-7-6
	 * Description 报表页 - 导出报告
	 * @param fileName 文件名称
	 * @param tableData table数据
	 */
	@RequestMapping(params="exportIrReport")
	public void exportIrReport(HttpServletRequest request,HttpServletResponse response,
							   @RequestParam(value="fileName",defaultValue="文件")String fileName,
							   @RequestParam(value="tableData",defaultValue="")String tableData)throws Exception{
		TSUser user=ResourceUtil.getSessionUserName();
		zyCommonService.exportIrReport(fileName,tableData,response);
		systemService.addLog(user.getRealName()+"导出:故障率报表", Globals.Log_Type_OTHER, Globals.Log_Leavel_INFO);
	}


	/**
	 * @author mhg
	 * 2018-7-6
	 * Description 跳转 弹出选择BP页
	 * @return
	 */
	@RequestMapping(params="goChooseBPReport")
	public ModelAndView goChooseBPReport(){
		return new ModelAndView("com/zybk/zy_report/zyChooseBPForReport");
	}
}
